class Repos
  attr_accessor :github, :collaborators, :commits, :downloads, :forks, :keys, :watching, :hooks

  def initialize(github)
    @github = github
    @collaborators = ReposCollaborators.new(github)
    @commits = ReposCommits.new(github)
    @downloads = ReposDownloads.new(github)
    @forks = ReposForks.new(github)
    @keys = ReposKeys.new(github)
    @watching = ReposWatching.new(github)
    @hooks = ReposHooks.new(github)
  end

  def listRepos(user=nil)
    username = user == nil ? @github.username : user
    url = 'users/%s/repos' % user != @github.username ? username : 'user/repos'
    @github.get(url)
  end

  def createUserRepo(name, description=nil, homepage=nil, private=FALSE, has_issues=TRUE, has_wiki=TRUE,
      has_downloads=TRUE)
    params = {
        :name => name,
        :private => private,
        :has_issues => has_issues,
        :has_wiki => has_wiki,
        :has_downloads => has_downloads,
        :description => description,
        :homepage => homepage
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.post('user/repos', data)
  end

  def listOrgRepos(org)
    @github.get('orgs/%s/repos' % org)
  end

  def createOrgRepo(org, name, description=nil, homepage=nil, private=FALSE,
      has_issues=TRUE, has_wiki=TRUE, has_downloads=TRUE)
    params = {
        :name => name,
        :private => private,
        :has_issues => has_issues,
        :has_wiki => has_wiki,
        :has_downloads => has_downloads,
        :description => description,
        :homepage => homepage
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.post('orgs/%s/repos' % org, data)
  end

  def getRepo(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s' % [username, repo])
  end

  def editRepo(repo, name, description=nil, homepage=nil, private=FALSE,
      has_issues=TRUE, has_wiki=TRUE, has_downloads=TRUE, user=nil)
    username = user == nil ? @github.username : user
    params = {
        :name => name,
        :description => description,
        :homepage => homepage,
        :private => private,
        :has_issues => has_issues,
        :has_wiki => has_wiki,
        :has_downloads => has_downloads
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.patch('repos/%s/%s' % [username, repo], data)
  end

  def listContributors(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get(
        'repos/%s/%s/contributors' % [username, repo])
  end

  def listLangs(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s/languages' % [username, repo])
  end

  def listTeams(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s/teams' % [username, repo])
  end

  def listTags(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s/tags' % [username, repo])
  end

  def listBranches(repo, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s/branches' % [username, repo])
  end
end
